package view.bookAdmiView;

import java.awt.EventQueue;


import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.eltima.components.ui.DatePicker;

import dao.BookDao;
import dao.BorrowImfDao;
import dao.UserDao;
import model.Book;
import model.BorrowImf;
import model.User;
import util.DbUtil;
import util.StringUtil;

import javax.swing.ButtonGroup;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.Date;

import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Vector;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.Dimension;

public class BookBorrowInterFrm extends JInternalFrame {
	private JTextField s_userNameTxt;
	private JTextField s_bookIdTxt;
	private JTextField s_bookNameTxt;
	private JTextField userNameTxt;
	private JTextField passWordTxt;
	private JTextField bookIdTxt;
	private JTextField bookNameTxt;
	private JTextField borrowTimeTxt;
	private JTextField returnTimeTxt;
	private JTextField renewNumTxt;
	private JTable userTable;
	private JTable bookTable;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JRadioButton notBorrowedJrb ;
	private JRadioButton borrowedJrb;
	
	
	private DbUtil dbUtil=new DbUtil();
	private UserDao userDao=new UserDao();
	private BookDao bookDao=new BookDao();
	private BorrowImfDao borrowImfDao=new BorrowImfDao();
	
	
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BookBorrowInterFrm frame = new BookBorrowInterFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BookBorrowInterFrm() {
		setBackground(new Color(224, 255, 255));
		setTitle("图书借阅");
		setClosable(true);
		setIconifiable(true);
		setBounds(100, 100, 1771, 885);
		
		JPanel userSearchPanel = new JPanel();
		userSearchPanel.setBackground(new Color(250, 235, 215));
		userSearchPanel.setBorder(new TitledBorder(null, "\u501F\u9605\u8005\u4FE1\u606F\u67E5\u8BE2", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JPanel bookSearchPanel = new JPanel();
		bookSearchPanel.setBackground(new Color(250, 235, 215));
		bookSearchPanel.setBorder(new TitledBorder(null, "\u56FE\u4E66\u4FE1\u606F\u67E5\u8BE2", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JScrollPane userScrollPane = new JScrollPane();
		
		JPanel bookBorrowPanel = new JPanel();
		bookBorrowPanel.setBackground(new Color(250, 235, 215));
		bookBorrowPanel.setBorder(new TitledBorder(null, "\u56FE\u4E66\u501F\u9605\u8868\u5355", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JScrollPane bookScrollPane = new JScrollPane();
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addComponent(bookBorrowPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(userSearchPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(bookSearchPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
					.addGap(37)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
						.addComponent(userScrollPane)
						.addComponent(bookScrollPane, GroupLayout.DEFAULT_SIZE, 842, Short.MAX_VALUE))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(29)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(userSearchPanel, GroupLayout.PREFERRED_SIZE, 101, GroupLayout.PREFERRED_SIZE)
							.addGap(30)
							.addComponent(bookSearchPanel, GroupLayout.PREFERRED_SIZE, 155, GroupLayout.PREFERRED_SIZE))
						.addComponent(userScrollPane, GroupLayout.PREFERRED_SIZE, 304, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(22)
							.addComponent(bookBorrowPanel, GroupLayout.PREFERRED_SIZE, 462, Short.MAX_VALUE)
							.addContainerGap(32, Short.MAX_VALUE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(42)
							.addComponent(bookScrollPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addContainerGap())))
		);
		
		bookTable = new JTable();
		bookTable.setFont(new Font("宋体", Font.BOLD, 20));
		
		bookTable.setBackground(new Color(250, 235, 215));//设置表格背景颜色
		bookTable.getTableHeader().setFont(new Font("微软雅黑",Font.BOLD,20));//设置表格开头字体
		bookTable.getTableHeader().setBackground(new Color(255, 192, 203));//设置表格开头背景颜色
		bookTable.setFont(new Font("楷体", Font.BOLD, 22));//设置表格内容字体
		bookTable.setForeground(new Color(0,0,0));//设置表格内容字体颜色		
		bookTable.setRowHeight(bookTable.getRowHeight()+20);//设置表格内容的行高
		
		bookTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent met) {
				bookTableMousePressed(met);
				
			}
		});
		bookTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u56FE\u4E66\u7F16\u53F7", "\u56FE\u4E66\u540D\u79F0", "\u4F5C\u8005", "\u56FE\u4E66\u7C7B\u522B", "\u501F\u9605\u72B6\u6001"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		bookScrollPane.setViewportView(bookTable);
		
		userTable = new JTable();
		userTable.setFont(new Font("宋体", Font.BOLD, 20));
		
		userTable.setBackground(new Color(250, 235, 215));//设置表格背景颜色
		userTable.getTableHeader().setFont(new Font("微软雅黑",Font.BOLD,20));//设置表格开头字体
		userTable.getTableHeader().setBackground(new Color(255, 192, 203));//设置表格开头背景颜色
		userTable.setFont(new Font("楷体", Font.BOLD, 22));//设置表格内容字体
		userTable.setForeground(new Color(0,0,0));//设置表格内容字体颜色		
		userTable.setRowHeight(userTable.getRowHeight()+20);//设置表格内容的行高
		
		
		userTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent met) {
				userTableMousePressed(met);
				
			}
		});
		userTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u7F16\u53F7", "\u7528\u6237\u540D", "\u7528\u6237\u5BC6\u7801", "\u6027\u522B", "\u624B\u673A\u53F7\u7801"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		userScrollPane.setViewportView(userTable);
		
		JLabel userName = new JLabel("用户名：");
		userName.setFont(new Font("楷体", Font.BOLD, 25));
		
		userNameTxt = new JTextField();
		userNameTxt.setEditable(false);
		userNameTxt.setFont(new Font("宋体", Font.BOLD, 25));
		userNameTxt.setColumns(10);
		
		JLabel passWord = new JLabel("用户密码：");
		passWord.setFont(new Font("楷体", Font.BOLD, 25));
		
		passWordTxt = new JTextField();
		passWordTxt.setEditable(false);
		passWordTxt.setFont(new Font("宋体", Font.BOLD, 25));
		passWordTxt.setColumns(10);
		
		JLabel bookId = new JLabel("图书编号：");
		bookId.setFont(new Font("楷体", Font.BOLD, 25));
		
		bookIdTxt = new JTextField();
		bookIdTxt.setEditable(false);
		bookIdTxt.setFont(new Font("宋体", Font.BOLD, 25));
		bookIdTxt.setColumns(10);
		
		JLabel bookName = new JLabel("图书名称：");
		bookName.setFont(new Font("楷体", Font.BOLD, 25));
		
		bookNameTxt = new JTextField();
		bookNameTxt.setEditable(false);
		bookNameTxt.setFont(new Font("宋体", Font.BOLD, 25));
		bookNameTxt.setColumns(10);
		
		JLabel borrowTime = new JLabel("借书日期：");
		borrowTime.setFont(new Font("楷体", Font.BOLD, 25));
		
		borrowTimeTxt = new JTextField(16);
		borrowTimeTxt.setFont(new Font("宋体", Font.BOLD, 25));
		
		DatePicker datepick;		
        datepick = StringUtil.getDatePicker();
        borrowTimeTxt.add( datepick);
        borrowTimeTxt.setText(datepick.getText());
        borrowTimeTxt.setVisible(true);	
        
		
		JLabel returnTime = new JLabel("应还日期：");
		returnTime.setFont(new Font("楷体", Font.BOLD, 25));
		
		returnTimeTxt = new JTextField();
		returnTimeTxt.setFont(new Font("宋体", Font.BOLD, 25));
		returnTimeTxt.setColumns(10);
		
		JLabel renewNum = new JLabel("续借次数：");
		renewNum.setFont(new Font("楷体", Font.BOLD, 25));
		
		renewNumTxt = new JTextField();
		renewNumTxt.setEditable(false);
		renewNumTxt.setFont(new Font("宋体", Font.BOLD, 25));
		renewNumTxt.setColumns(10);
		
		JLabel status = new JLabel("借阅状态：");
		status.setFont(new Font("楷体", Font.BOLD, 25));
		
		notBorrowedJrb = new JRadioButton("在馆");
		buttonGroup.add(notBorrowedJrb);
		notBorrowedJrb.setSelected(true);
		notBorrowedJrb.setFont(new Font("楷体", Font.BOLD, 25));
		
		borrowedJrb = new JRadioButton("已借");
		buttonGroup.add(borrowedJrb);
		borrowedJrb.setFont(new Font("楷体", Font.BOLD, 25));
		
		JButton borrowButton = new JButton("借阅");
		borrowButton.setIcon(new ImageIcon(BookBorrowInterFrm.class.getResource("/images/bookAdmi/借阅 (1).png")));
		borrowButton.setFont(new Font("宋体", Font.BOLD, 25));
		borrowButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				borrowImfAddActionPerformed(e);
			}
		});
		
		JButton cancelButton = new JButton("取消");
		cancelButton.setIcon(new ImageIcon(BookBorrowInterFrm.class.getResource("/images/bookAdmi/取消.png")));
		cancelButton.setFont(new Font("宋体", Font.BOLD, 25));
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				borrowImfResetActionPerformed(e);
			}
		});
		GroupLayout gl_bookBorrowPanel = new GroupLayout(bookBorrowPanel);
		gl_bookBorrowPanel.setHorizontalGroup(
			gl_bookBorrowPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_bookBorrowPanel.createSequentialGroup()
					.addGroup(gl_bookBorrowPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_bookBorrowPanel.createSequentialGroup()
							.addGap(27)
							.addGroup(gl_bookBorrowPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(renewNum)
								.addGroup(gl_bookBorrowPanel.createSequentialGroup()
									.addComponent(userName)
									.addGap(18)
									.addComponent(userNameTxt, GroupLayout.PREFERRED_SIZE, 168, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_bookBorrowPanel.createSequentialGroup()
									.addGroup(gl_bookBorrowPanel.createParallelGroup(Alignment.TRAILING, false)
										.addComponent(bookId, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(borrowTime, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(gl_bookBorrowPanel.createParallelGroup(Alignment.LEADING)
										.addComponent(renewNumTxt, GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
										.addGroup(gl_bookBorrowPanel.createParallelGroup(Alignment.TRAILING, false)
											.addComponent(borrowTimeTxt, Alignment.LEADING, 0, 0, Short.MAX_VALUE)
											.addComponent(bookIdTxt, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)))))
							.addGap(173)
							.addGroup(gl_bookBorrowPanel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_bookBorrowPanel.createSequentialGroup()
									.addComponent(status)
									.addGap(8)
									.addComponent(notBorrowedJrb)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(borrowedJrb))
								.addGroup(gl_bookBorrowPanel.createSequentialGroup()
									.addComponent(returnTime)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(returnTimeTxt, 164, 164, 164))
								.addGroup(gl_bookBorrowPanel.createSequentialGroup()
									.addComponent(bookName)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(bookNameTxt, 164, 164, 164))
								.addGroup(gl_bookBorrowPanel.createSequentialGroup()
									.addComponent(passWord)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(passWordTxt, GroupLayout.PREFERRED_SIZE, 149, GroupLayout.PREFERRED_SIZE))))
						.addGroup(gl_bookBorrowPanel.createSequentialGroup()
							.addGap(141)
							.addComponent(borrowButton)
							.addGap(127)
							.addComponent(cancelButton)))
					.addContainerGap())
		);
		gl_bookBorrowPanel.setVerticalGroup(
			gl_bookBorrowPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_bookBorrowPanel.createSequentialGroup()
					.addGap(32)
					.addGroup(gl_bookBorrowPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(userName)
						.addComponent(userNameTxt, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
						.addComponent(passWord)
						.addComponent(passWordTxt, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
					.addGap(46)
					.addGroup(gl_bookBorrowPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(bookId)
						.addComponent(bookName)
						.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
						.addComponent(bookIdTxt, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE))
					.addGroup(gl_bookBorrowPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_bookBorrowPanel.createSequentialGroup()
							.addGap(47)
							.addGroup(gl_bookBorrowPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(borrowTime)
								.addComponent(returnTime)
								.addComponent(borrowTimeTxt, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_bookBorrowPanel.createSequentialGroup()
							.addGap(46)
							.addComponent(returnTimeTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(48)
					.addGroup(gl_bookBorrowPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(renewNum)
						.addComponent(renewNumTxt, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
						.addComponent(status)
						.addComponent(notBorrowedJrb)
						.addComponent(borrowedJrb))
					.addGap(39)
					.addGroup(gl_bookBorrowPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(borrowButton)
						.addComponent(cancelButton))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		bookBorrowPanel.setLayout(gl_bookBorrowPanel);
		
		JLabel s_bookId = new JLabel("图书编号：");
		s_bookId.setFont(new Font("宋体", Font.BOLD, 25));
		
		s_bookIdTxt = new JTextField();
		s_bookIdTxt.setFont(new Font("宋体", Font.BOLD, 25));
		s_bookIdTxt.setColumns(10);
		
		JLabel s_bookName = new JLabel("图书名称：");
		s_bookName.setFont(new Font("宋体", Font.BOLD, 25));
		
		s_bookNameTxt = new JTextField();
		s_bookNameTxt.setFont(new Font("宋体", Font.BOLD, 25));
		s_bookNameTxt.setColumns(10);
		
		JButton bookSearchButton = new JButton("查询");
		bookSearchButton.setIcon(new ImageIcon(BookBorrowInterFrm.class.getResource("/images/bookAdmi/查询.png")));
		bookSearchButton.setFont(new Font("宋体", Font.BOLD, 25));
		bookSearchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bookSearchActionPerformed(e);
				
			}
		});
		
		JButton bookResetButton = new JButton("重置");
		bookResetButton.setFont(new Font("宋体", Font.BOLD, 25));
		bookResetButton.setIcon(new ImageIcon(BookBorrowInterFrm.class.getResource("/images/bookAdmi/重置.png")));
		bookResetButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bookSearchResetActionPerformed(e);
			}
		});
		GroupLayout gl_bookSearchPanel = new GroupLayout(bookSearchPanel);
		gl_bookSearchPanel.setHorizontalGroup(
			gl_bookSearchPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_bookSearchPanel.createSequentialGroup()
					.addGroup(gl_bookSearchPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_bookSearchPanel.createSequentialGroup()
							.addGap(24)
							.addComponent(s_bookId)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(s_bookIdTxt, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE)
							.addGap(59)
							.addComponent(s_bookName)
							.addGap(14)
							.addComponent(s_bookNameTxt, GroupLayout.PREFERRED_SIZE, 143, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_bookSearchPanel.createSequentialGroup()
							.addGap(223)
							.addComponent(bookSearchButton)
							.addGap(109)
							.addComponent(bookResetButton)))
					.addContainerGap(178, Short.MAX_VALUE))
		);
		gl_bookSearchPanel.setVerticalGroup(
			gl_bookSearchPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_bookSearchPanel.createSequentialGroup()
					.addGap(29)
					.addGroup(gl_bookSearchPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(s_bookId)
						.addComponent(s_bookIdTxt, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
						.addComponent(s_bookName)
						.addComponent(s_bookNameTxt, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_bookSearchPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(bookResetButton)
						.addComponent(bookSearchButton))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		bookSearchPanel.setLayout(gl_bookSearchPanel);
		
		JLabel s_userName = new JLabel("用户名：");
		s_userName.setFont(new Font("宋体", Font.BOLD, 25));
		
		s_userNameTxt = new JTextField();
		s_userNameTxt.setFont(new Font("宋体", Font.BOLD, 25));
		s_userNameTxt.setColumns(10);
		
		JButton userSearchButton = new JButton("查询");
		userSearchButton.setIcon(new ImageIcon(BookBorrowInterFrm.class.getResource("/images/bookAdmi/查询.png")));
		userSearchButton.setFont(new Font("宋体", Font.BOLD, 25));
		userSearchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				userSearchActionPerformed(e);
				
			}
		});
		
		JButton userResetButton = new JButton("重置");
		userResetButton.setFont(new Font("宋体", Font.BOLD, 25));
		userResetButton.setIcon(new ImageIcon(BookBorrowInterFrm.class.getResource("/images/bookAdmi/重置.png")));
		userResetButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				userSearchResetActionperformed(e);
			}
		});
		GroupLayout gl_userSearchPanel = new GroupLayout(userSearchPanel);
		gl_userSearchPanel.setHorizontalGroup(
			gl_userSearchPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_userSearchPanel.createSequentialGroup()
					.addGap(22)
					.addComponent(s_userName)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(s_userNameTxt, GroupLayout.PREFERRED_SIZE, 153, GroupLayout.PREFERRED_SIZE)
					.addGap(82)
					.addComponent(userSearchButton)
					.addGap(62)
					.addComponent(userResetButton)
					.addContainerGap(149, Short.MAX_VALUE))
		);
		gl_userSearchPanel.setVerticalGroup(
			gl_userSearchPanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_userSearchPanel.createSequentialGroup()
					.addContainerGap(18, Short.MAX_VALUE)
					.addGroup(gl_userSearchPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_userSearchPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(s_userNameTxt, GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
							.addComponent(userSearchButton)
							.addComponent(userResetButton))
						.addComponent(s_userName))
					.addGap(26))
		);
		userSearchPanel.setLayout(gl_userSearchPanel);
		getContentPane().setLayout(groupLayout);
		
		
		//设置JFrame居中显示
		this.setLocation(80, 0);
				
		this.fillUserTable(new User());
		this.fillBookTable2(new Book());
	}
	
	/**
	 * 添加借阅事件处理
	 * @param e
	 */
	private void borrowImfAddActionPerformed(ActionEvent evt) {
		String userName=this.userNameTxt.getText();
		if(StringUtil.isEmpty(userName)) {
			JOptionPane.showMessageDialog(null, "请选择数据！");
		}
		else {
			String passWord=this.passWordTxt.getText();
			int bookId=Integer.parseInt(this.bookIdTxt.getText());
			String bookName=this.bookNameTxt.getText();		
			int renewNum=Integer.parseInt(this.renewNumTxt.getText());
			String status="";
			if(notBorrowedJrb.isSelected()) {
				status="在馆";
				
			}
			else if(borrowedJrb.isSelected()) {
				status="已借";
			}
			
			//String borrowTime2=this.borrowTimeTxt.getText();
			String borrowTime2=this.borrowTimeTxt.getText();
			java.sql.Date borrowTime=java.sql.Date.valueOf(borrowTime2);
			
			String returnTime2=this.returnTimeTxt.getText();
			java.sql.Date returnTime=java.sql.Date.valueOf(returnTime2);
			
			BorrowImf borrowImf=new BorrowImf(userName,bookName,borrowTime,returnTime,renewNum,bookId,status, passWord);
			Book book =new Book(bookId,bookName,status);
			
			
				Connection con=null;
				try {
					con=dbUtil.getCon();
					int addNum=borrowImfDao.add(con,borrowImf);			
					if(addNum==1 ) {
						JOptionPane.showMessageDialog(null, "图书借阅成功!");
						bookDao.update2(con,book);//将借阅图书状态更新为已借
						reSetBorrowImfValue();
						
						
						this.fillBookTable2(new Book());
						
						
						
						
					}
					else {
						JOptionPane.showMessageDialog(null, "图书借阅失败");
					}
				}catch(Exception e) {
					e.printStackTrace();
					JOptionPane.showMessageDialog(null, "图书借阅失败");
				}finally {
					try {
						dbUtil.closeCon(con);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			
			
		}
		
	}

	/**
	 * 借阅表单取消事件处理
	 * @param e
	 */
	private void borrowImfResetActionPerformed(ActionEvent evt) {
		this.reSetBorrowImfValue() ;
	}

	
	/**
	 * 重置借阅表单处理
	 */
	private void reSetBorrowImfValue() {
		this.userNameTxt.setText("");
		this.passWordTxt.setText("");
		this.bookIdTxt.setText("");
		this.bookNameTxt.setText("");
		this.borrowTimeTxt.setText("");
		this.returnTimeTxt.setText("");
		this.renewNumTxt.setText("");
		this.notBorrowedJrb.setSelected(true);
		
	}
	
	/**
	 * 图书信息查询取消事件处理
	 * @param e
	 */
	private void bookSearchResetActionPerformed(ActionEvent e) {
		this.s_bookIdTxt.setText("");
		this.s_bookNameTxt.setText("");
		
	}

	/**
	 * 用户信息查询取消事件处理
	 * @param e
	 */
	private void userSearchResetActionperformed(ActionEvent e) {
		this.s_userNameTxt.setText("");
		
	}

	/**
	 * 用户信息表格行点击事件处理
	 * @param me
	 */
	protected void userTableMousePressed(MouseEvent met) {
		int row=this.userTable.getSelectedRow();
		this.userNameTxt.setText((String)userTable.getValueAt(row, 1));
		this.passWordTxt.setText((String)userTable.getValueAt(row, 2));
		
		
	}
	
	/**
	 * 图书信息表格行点击事件处理
	 * @param me
	 */
	private void bookTableMousePressed(MouseEvent met) {
		int row=this.bookTable.getSelectedRow();
		this.bookIdTxt.setText((String)bookTable.getValueAt(row, 0));
		this.bookNameTxt.setText((String)bookTable.getValueAt(row, 1));
		String status=(String)bookTable.getValueAt(row, 4);
		if("在馆".equals(status)) {
			this.notBorrowedJrb.setSelected(true);
		}
	   else if("已借".equals(status)) {
		this.borrowedJrb.setSelected(true);
	   }
		this.renewNumTxt.setText("0");
		if(status.contentEquals("已借")) {
			JOptionPane.showMessageDialog(null, "请选择正确的图书！");
			this.bookIdTxt.setText("");
			this.bookNameTxt.setText("");
		}
	}
	
	

	
	/**
	 * 图书信息查询事件处理
	 * @param e
	 */
	private void bookSearchActionPerformed(ActionEvent evt) {
		
		if(StringUtil.isEmpty(this.s_bookIdTxt.getText())) {
			JOptionPane.showMessageDialog(null, "请正确输入图书编号");
			
		}
		else {
			int id=Integer.parseInt(this.s_bookIdTxt.getText());		
			String bookName=this.s_bookNameTxt.getText();
			String status="在馆";//因为借阅的图书一定都是在馆的图书，所以在此设下查询限制，只显示在馆的图书，不显示已借出的图书
			Book book=new Book(id,bookName,status);
			this.fillBookTable(book);
		}
		
	}

	/**
	 * 查询借阅者信息事件处理
	 * @param e
	 */
	private void userSearchActionPerformed(ActionEvent e) {
		String userName=this.s_userNameTxt.getText();
		User user=new User(userName);
		this.fillUserTable(user);
	}
	
	/**
	 * 初始化用户信息表格数据
	 * @param user
	 */
	private void fillUserTable(User user) {
		DefaultTableModel dtm=(DefaultTableModel) userTable.getModel();
		dtm.setRowCount(0);
		Connection con=null;
		try {
			con=dbUtil.getCon();
			ResultSet rs=userDao.list(con, user);
			while(rs.next()) {
				Vector v=new Vector();
				v.add(rs.getString("id"));
				v.add(rs.getString("username"));
				v.add(rs.getString("passWord"));
				v.add(rs.getString("sex"));
				v.add(rs.getString("tel"));
				dtm.addRow(v);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
		
		/**
		 * 初始化图书信息表格数据
		 * @param book
		 */
		private void fillBookTable(Book book) {
			DefaultTableModel dtm=(DefaultTableModel) bookTable.getModel();
			dtm.setRowCount(0);//设置成0行
			Connection con=null;
			try {
				con=dbUtil.getCon();
				ResultSet rs=bookDao.list2(con, book);
				while(rs.next()) {
					Vector v=new Vector();
					v.add(rs.getString("id"));
					v.add(rs.getString("bookName"));
					v.add(rs.getString("author"));
					v.add(rs.getString("bookTypeName"));
					v.add(rs.getString("status"));
					dtm.addRow(v);
				}
			}catch(Exception e) {
				e.printStackTrace();
			}finally {
				try {
					dbUtil.closeCon(con);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}		
	}
		
		private void fillBookTable2(Book book) {
			DefaultTableModel dtm=(DefaultTableModel) bookTable.getModel();
			dtm.setRowCount(0);//设置成0行
			Connection con=null;
			try {
				con=dbUtil.getCon();
				ResultSet rs=bookDao.list(con, book);
				while(rs.next()) {
					Vector v=new Vector();
					v.add(rs.getString("id"));
					v.add(rs.getString("bookName"));
					v.add(rs.getString("author"));
					v.add(rs.getString("bookTypeName"));
					v.add(rs.getString("status"));
					dtm.addRow(v);
				}
			}catch(Exception e) {
				e.printStackTrace();
			}finally {
				try {
					dbUtil.closeCon(con);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}		
	}
		
}
